package com.lzxzjy.serivce.impl;

import com.lzxzjy.mapper.AdminMapper;
import com.lzxzjy.mapper.BorrowInfoMapper;
import com.lzxzjy.mapper.ReaderMapper;
import com.lzxzjy.pojo.BorrowInfo;
import com.lzxzjy.pojo.PageBean;
import com.lzxzjy.pojo.Reader;
import com.lzxzjy.serivce.ReaderService;
import com.lzxzjy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ReaderServiceImpl implements ReaderService {
    // 获取工厂对象
    private final SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public boolean insertReader(Reader reader) {
        SqlSession sqlSession = factory.openSession();
        ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class);
        Integer integer = readerMapper.insertReader(reader);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    @Override
    public PageBean<Reader> selectReaderByPageAndTerms(Reader reader, int currentPage, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class);

        int begin = (currentPage - 1) * pageSize;

        List<Reader> readers = readerMapper.selectReaderByPageAndTerms(reader, begin, pageSize);
        int totalCount = readerMapper.selectReaderTotalCountByTerms(reader);
        BorrowInfoMapper borrowInfoMapper = sqlSession.getMapper(BorrowInfoMapper.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        for (Reader r : readers) {
            List<BorrowInfo> borrowInfos = borrowInfoMapper.selectBorrowInfoByPageAndBookTerms(new BorrowInfo(null, "%%", r.getReaderId(), "%%", "", "", null), 0, 9);
            double readerArrears = 0.0;
            for (BorrowInfo b : borrowInfos) {
                try {
                     readerArrears += ((now.getTime() - simpleDateFormat.parse(b.getReturnTime()).getTime()) / (1000 * 60 * 60 * 24) < 0 ? 0 : (double) (simpleDateFormat.parse(simpleDateFormat.format(now.getTime())).getTime() - simpleDateFormat.parse(b.getReturnTime()).getTime()) / (1000 * 60 * 60 * 24)) * 0.2;
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
            r.setReaderArrears(readerArrears);
        }
        PageBean<Reader> readerPageBean = new PageBean<>();
        readerPageBean.setRows(readers);
        readerPageBean.setTotalCount(totalCount);
        sqlSession.close();
        return readerPageBean;
    }

    @Override
    public boolean deleteReaderByIds(int[] readerIds) {
        SqlSession sqlSession = factory.openSession();
        ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class);

        Integer integer = readerMapper.deleteReaderByIds(readerIds);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean deleteReaderById(int readerId) {
        SqlSession sqlSession = factory.openSession();
        ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class);

        Integer integer = readerMapper.deleteReaderById(readerId);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean updateReaderById(Reader reader) {
        SqlSession sqlSession = factory.openSession();
        ReaderMapper readerMapper = sqlSession.getMapper(ReaderMapper.class);

        Integer integer = readerMapper.updateReaderById(reader);

        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    @Override
    public Reader selectReaderByUsernameAndPassWord(String username, String password) {
        SqlSession readerSqlSession = factory.openSession();
        ReaderMapper readerMapper = readerSqlSession.getMapper(ReaderMapper.class);
        Reader reader = readerMapper.selectReaderByUsernameAndPassWord(username, password);
        readerSqlSession.close();
        return reader;
    }

    @Override
    public boolean selectReaderEmailByEmail(String email) {
        SqlSession readerSqlSession = factory.openSession();
        ReaderMapper readerMapper = readerSqlSession.getMapper(ReaderMapper.class);
        Integer integer = readerMapper.selectReaderEmailByEmail(email);
        return integer == 0;
    }

    @Override
    public boolean selectReaderPhoneByPhone(String readerPhone) {
        SqlSession readerSqlSession = factory.openSession();
        ReaderMapper readerMapper = readerSqlSession.getMapper(ReaderMapper.class);
        Integer integer = readerMapper.selectReaderPhoneByPhone(readerPhone);
        return integer == 0;
    }
}
